<template>
	<view class="loginclass">
		<uni-section title="用户名" type="line" padding>
			<uni-easyinput :styles="inputstyles" errorMessage v-model="loginval.name" trim
				placeholder="请输入用户名"></uni-easyinput>
		</uni-section>
		<uni-section title="密码" type="line" padding>
			<uni-easyinput type="password" :styles="inputstyles" errorMessage v-model="loginval.password" trim
				placeholder="请输入密码"></uni-easyinput>
		</uni-section>
		<view style="display: flex;padding:100rpx 20rpx;justify-content: space-between;">
			<view class="login" @click="login">登录</view>
			<view class="notuser" @click="notuser">注册并登录</view>
		</view>
	</view>

</template>

<script setup>
	const app = getApp();
	var inputstyles = {
		color: "#666"
	}
	var loginval = {
		name: "",
		password: ""
	}
	// 注册
	const notuser = () => {
		if (!loginval.name || !loginval.password) {
			uni.showToast({
				icon: 'none',
				title: '用户名或密码不能为空'
			})
		} else {
			app.apiPost(app.globalData.apiList.adduser, loginval, (res) => {
				if(res.code == 1){
					login()
				}
			})
		}
	}
	// 登录
	const login = () => {
		if (!loginval.name || !loginval.password) {
			uni.showToast({
				icon: 'none',
				title: '用户名或密码不能为空'
			})
		} else {
			app.apiPost(app.globalData.apiList.login, loginval, (res) => {
				if(res.code == 1){
					var token = res.data.token
					uni.setStorageSync('access-token',token)
					uni.showToast({
						title:"登录成功",
						icon:"none"
					})
					setTimeout(()=>{
						uni.reLaunch({
							url:'/pages/index/index'
						})
					},500)
				}
			})
		}
	}
</script>

<style lang="scss" scoped>
	.loginclass {
		padding: 30rpx 0;

		.login {
			width: 250rpx;
			height: 100rpx;
			border-radius: 12rpx;
			color: #fff;
			font-size: 40rpx;
			text-align: center;
			line-height: 100rpx;
			font-weight: bold;
			background-color: #f60;
		}

		.notuser {
			width: 450rpx;
			height: 100rpx;
			border-radius: 12rpx;
			color: #fff;
			font-size: 40rpx;
			text-align: center;
			line-height: 100rpx;
			font-weight: bold;
			background-color: #2979ff;
		}
	}
</style>